/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import healthcare.coursework.model.Users;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.FacilityService;
import healthcare.coursework.service.MedicalHistoryService;
import healthcare.coursework.service.PatientService;
import healthcare.coursework.service.UserService;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 *
 * @author Administrator
 */
public class Utility {

    private static ApplicationContext ctx;
    private UserService userService;
    private PatientService patientService;
    private AppointmentService appointmentService;
    private MedicalHistoryService medicalHistoryService;
    private FacilityService facilityService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserService getUserService() {
        return userService;
    }

    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }

    public AppointmentService getAppointmentService() {
        return appointmentService;
    }

    public void setAppointmentService(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    public FacilityService getFacilityService() {
        return facilityService;
    }

    public void setFacilityService(FacilityService facilityService) {
        this.facilityService = facilityService;
    }

    public void setMedicalHistoryService(MedicalHistoryService medicalHistoryService) {
        this.medicalHistoryService = medicalHistoryService;
    }

    public MedicalHistoryService getMedicalHistoryService() {
        return medicalHistoryService;
    }

    static {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    public static Utility getUtility() {
        return (Utility) ctx.getBean("utility", util.Utility.class);
    }

    public static String getUsername() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principal != null) {
                if (principal instanceof UserDetails) {
                    return ((UserDetails) principal).getUsername();
                }
            }
            return null;
        } catch (NullPointerException e) {
            System.out.println("Cannot find user");
            return null;
        }
    }

    public static UserDetails getUserDetails() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal != null) {
            if (principal instanceof UserDetails) {
                return ((UserDetails) principal);
            }
        }
        return null;
    }

    public static Date convertToDate(Date day, Date time) {
        day.setHours(time.getHours());
        day.setMinutes(time.getMinutes());
        day.setSeconds(time.getSeconds());
        return day;
    }

    public static Timestamp convertToTimestamp(Date d) {
        return new Timestamp(d.getTime());
    }

      /**
     * Canculate number of days counting from now
     * @param t
     *  the date to be compared
     * @return
     *  number of days between now and the inputed date
     */
    public static double calculateDaysDiff(Timestamp t) {
        if (t==null) return 0;
        Calendar now = Calendar.getInstance();
        long mil = 24 * 3600 * 1000;
        double days = ((double) now.getTimeInMillis() - t.getTime()) / mil;
        return days;
    }

     public static List<Users> filterRole(List<Users> staffs,String role){
        for (int i = 0; i < staffs.size(); i++) {
              Users user=staffs.get(i);
            if(user.getListAuthorities().equals(role))
               continue;
            else
            {
                staffs.remove(i);
                System.out.println("Remove "+user.getUsername());
            }

        }
        return staffs;
    }
}
